package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.inter.OnCBCheckedChangeListener;
import com.wangtao.firstapp.modles.CategoryClassEntity;

/**
 * 分类下模块二级页面顶部CheckBox数据的适配器
 * Created by lanouhn on 16/9/6.
 */
public class CategoryClassAdapter extends RecyclerView.Adapter<CategoryClassAdapter.ViewHolder> {
    private Context context;
    private CategoryClassEntity categoryClassEntity;
    private OnCBCheckedChangeListener onCBCheckedChangeListener;

    public void setOnCBCheckedChangeListener(OnCBCheckedChangeListener onCBCheckedChangeListener) {
        this.onCBCheckedChangeListener = onCBCheckedChangeListener;
    }

    public CategoryClassAdapter(Context context, CategoryClassEntity categoryClassEntity) {
        this.context = context;
        this.categoryClassEntity = categoryClassEntity;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_horizontalsrollview_categoryclass, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        Picasso.with(context).load(categoryClassEntity.getData().getSubclass_list().get(position).getPic()).config(Bitmap.Config.RGB_565).into(holder.iv_categoryclass);
        holder.cb_categoryclass.setText(categoryClassEntity.getData().getSubclass_list().get(position).getName());
        if (onCBCheckedChangeListener != null) {
            holder.cb_categoryclass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    String ids = categoryClassEntity.getData().getSubclass_list().get(position).getId();
                    onCBCheckedChangeListener.OnCBCheckedChangeListener(ids, isChecked);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return categoryClassEntity.getData().getSubclass_list().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_categoryclass;
        private CheckBox cb_categoryclass;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_categoryclass = (ImageView) itemView.findViewById(R.id.iv_categoryclass);
            cb_categoryclass = (CheckBox) itemView.findViewById(R.id.cb_categoryclass);
        }
    }

}
